package com.bwei.like.mynewstop.manager;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import com.bwei.like.mynewstop.Bean.CityInfo;
import com.bwei.like.mynewstop.R;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by LiKe on 2016/11/14.
 */
public class DbManager {

    private Context context;
    private final File targetFile;
    private BufferedOutputStream bufferedOutputStream;

    public DbManager(Context context) {
        this.context = context;
        //获取Sd卡路劲
        File externalStorageDirectory = Environment.getExternalStorageDirectory();

        File dirFile = new File(externalStorageDirectory, "toutiaodb");

        //判断是否有该文件夹，
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }

        targetFile = new File(dirFile, "citydb");
    }

    //将数据库文件拷贝到Sd卡目录下
    public void cope() {
        InputStream inputStream = context.getResources().openRawResource(R.raw.china_city_name);
        //创建输出流，将文件写入到sd中
        try {
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, len);
                bufferedOutputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedOutputStream.close();
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //定义方法将数据库中内容读取出来
    ArrayList<CityInfo> cityInfoList = new ArrayList<>();

    public ArrayList<CityInfo> getCityList() {
        //打开数据库
        SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(targetFile, null);
        //开始读取数据库中数据
        Cursor cursor = sqLiteDatabase.rawQuery("select * from T_city", null);
        while (cursor.moveToNext()) {
            String cityName = cursor.getString(cursor.getColumnIndex("CityName"));
            String nameSort = cursor.getString(cursor.getColumnIndex("NameSort"));
            cityInfoList.add(new CityInfo(cityName, nameSort));
        }
        //将集合中数据按照正序进行排序
        Collections.sort(cityInfoList);

        for (int i = 0; i < cityInfoList.size(); i++) {
            if (i == 0) {
                cityInfoList.get(i).setFlag(true);
            } else {
                if (cityInfoList.get(i - 1).getNameSort().equals(cityInfoList.get(i).getNameSort())) {
                    cityInfoList.get(i).setFlag(false);
                } else {
                    cityInfoList.get(i).setFlag(true);
                }
            }
        }
        return cityInfoList;
    }
}
